package com.xiaoliang.servlet;

import com.xiaoliang.entry.Product;
import com.xiaoliang.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author XIAOLIANG
 * @ClassName UpdateProduct.java
 * @Description TODO
 * @createTime 2021年02月26日 11:43:00
 */
//@WebServlet("/product/update")
public class UpdateProduct extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		String id = req.getParameter("id").trim();
		String name = req.getParameter("name").trim();
		String price = req.getParameter("price").trim();
		String count = req.getParameter("count").trim();
		String description = req.getParameter("description").trim();
		ProductServiceImpl productService = new ProductServiceImpl();

//		System.out.println(id);
//		System.out.println(name);
//		System.out.println(price);
//		System.out.println(count);
//		System.out.println(description);

		Product product = new Product(Integer.parseInt(id), name, Double.parseDouble(price), Integer.parseInt(count), 0, description);

		boolean b = productService.updateProduct(product);

		if (b){
			System.out.println("更新成功");
			resp.sendRedirect(req.getContextPath()+"/product/queryList");
		}else {
			HttpSession session = req.getSession();
			session.setAttribute("msg","更新失败");
			resp.sendRedirect(req.getContextPath()+"/product/addOrUpdate.jsp");
		}



	}
}
